package com.ct.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ct.product.dao.CategoryDao;
import com.ct.product.entity.CategoryEntity;
import com.ct.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }
    /*
    * 查所有分类
    * 组装父子的树形结构
    * */
    @Override
    public List<CategoryEntity> listWithTree() {
        //查所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        List<CategoryEntity> temp = categoryEntities;
        //2组装树形结构
        //3找到所有一级分类
        List<CategoryEntity> level1MenusCollect = categoryEntities.stream().filter(new Predicate<CategoryEntity>() {
            @Override
            public boolean test(CategoryEntity categoryEntity) {
                return categoryEntity.getParentCid() == 0;
            }
        }).map((menu)->{
            menu.setChildren(getChildren(menu,categoryEntities));
            return menu;
        }).sorted((menu,menu2)->{
            return menu.getSort()-menu2.getSort();
        }).collect(Collectors.toList());
        /*
        * (menu)->{
            menu.setChildren(null);
            return menu;
        }
//
//        //4找到所有二级分类
//        //5组装号二级菜单
//        //找到三级菜单
//
//        for (CategoryEntity categoryEntity : level1MenusCollect) {
//            ListIterator<CategoryEntity> categoryEntityListIterator = categoryEntity.getChildren().listIterator();
//            while(categoryEntityListIterator.hasNext()){
//                CategoryEntity next = categoryEntityListIterator.next();
//                List<CategoryEntity> collect = categoryEntities1.parallelStream().filter(
//                        child -> {
//                            return child.getParentCid() == next.getCatId();
//                        }
//                ).collect(Collectors.toList());
//                next.setChildren(collect);
//            }
//        }
//        level1MenusCollect.forEach(n->{//1
//            List<CategoryEntity> children = n.getChildren();
//            List<CategoryEntity> collect1 = n.getChildren().stream().map(n1 -> {
//                List<CategoryEntity> collect = categoryEntities1.stream().filter(
//                        child -> {
//                            return child.getParentCid() == n1.getCatId();
//                        }
//                ).collect(Collectors.toList());
//                n1.setChildren(collect);
//                return n1;
//            }).collect(Collectors.toList());
//        });
//        for (CategoryEntity categoryEntity : level1MenusCollect) {
//            List<CategoryEntity> children = categoryEntity.getChildren();
//            for (CategoryEntity child : children) {
//                ArrayList<CategoryEntity> categoryEntities1 = new ArrayList<>();
//                for (CategoryEntity entity : temp) {
//                    if (entity.getParentCid()==child.getCatId()){
//                        categoryEntities1.add(entity);
//                    }
//                }
//                child.setChildren(categoryEntities1);
//            }
//        }


* */
        int i=0;
        int j=0;
        for (CategoryEntity categoryEntity : level1MenusCollect) {
            List<CategoryEntity> children = categoryEntity.getChildren();
            j+=children.size();
            for (CategoryEntity child : children) {
                i+=child.getChildren().size();
            }
        }
        System.out.println("三级"+i);
        System.out.println("二级"+j);
        System.out.println("一级"+level1MenusCollect.size());

        return level1MenusCollect;
    }

    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity->{
           categoryEntity.setChildren(getChildren(categoryEntity,all));
           return categoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return collect;
    }
}